package com.bentley.kotlindemo.ui.kits.lefttab

import android.graphics.Color
import android.os.Bundle
import android.view.Menu
import android.view.MenuInflater
import androidx.drawerlayout.widget.DrawerLayout
import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.navigateUp
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.bentley.kotlindemo.R
import com.bentley.kotlindemo.base.BaseActivity
import com.bentley.kotlindemo.databinding.ActivityLefttabBinding
import com.bentley.kotlindemo.utils.StatusBarUtils
import com.google.android.material.navigation.NavigationView

/**
 * <pre>
 *  ┌────────────────────────────────────────────
 *  │ @author  Lee  @date 2021/3/26 14:16
 *  ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
 *  │ @desc  左侧划入划出抽屉
 *  └────────────────────────────────────────────
 * </pre>
 */
class LeftTabActivity : BaseActivity<ActivityLefttabBinding, LeftTabModel>() {

    private lateinit var drawerLayout: DrawerLayout
    private lateinit var appBarConfiguration: AppBarConfiguration

    override fun initView(savedInstanceState: Bundle?) {

        setupNavigationDrawer()
        setSupportActionBar(findViewById(R.id.toolbar))

        val navHostFragment =
            supportFragmentManager.findFragmentById(R.id.left_nav_host_fragment) as NavHostFragment
        val navController = navHostFragment.navController
//
        //关联抽屉
        val navView = findViewById<NavigationView>(R.id.nav_view)
                as NavigationView
        navView.setupWithNavController(navController)


        //抽屉点击
        navView.findViewById<NavigationView>(R.id.nav_view).setNavigationItemSelectedListener {
            dataBinding.drawerLayout.closeDrawers()
            when (it.itemId) {

                R.id.home -> {
                    findNavController(R.id.left_nav_host_fragment)
                        .navigate(R.id.navigation_movie)
                }
                R.id.blog -> {
                    findNavController(R.id.left_nav_host_fragment).navigate(R.id.navigation_music)
                }
            }
            true
        }


        //关联顶部导航
        appBarConfiguration =
            AppBarConfiguration.Builder(R.id.navigation, R.id.navigation)
                .setDrawerLayout(dataBinding.drawerLayout)
                .build()
        setupActionBarWithNavController(navController, appBarConfiguration)

        StatusBarUtils.setStatusBarColor(this,Color.parseColor("#00BFFF"))
        StatusBarUtils.setStatusBarLightMode(this,false)

    }

    override fun onSupportNavigateUp(): Boolean {
        return findNavController(R.id.left_nav_host_fragment).navigateUp(appBarConfiguration) ||
                super.onSupportNavigateUp()
    }

    private fun setupNavigationDrawer() {
        drawerLayout = (findViewById<DrawerLayout>(R.id.drawer_layout))
            .apply {
//                setStatusBarBackground(R.color.design_default_color_on_primary)
            }
    }

//    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
//        inflater.inflate(R.menu.tasks_fragment_menu, menu)
//    }

    override fun getLayoutId(): Int = R.layout.activity_lefttab
}